package com.yuntongxun.ecdemo.voicechat.interactor.impl;

import android.app.Activity;
import android.content.Context;
import android.util.Log;


import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.interactor.ICommonRequestInteractor;
import com.yuntongxun.ecdemo.voicechat.listeners.IRequestListener;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.http.AsyncHttpHelper;

import java.io.File;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**
 * Created by liucanwen on 16/1/23.
 */
public class CommonRequestInteractorImpl implements ICommonRequestInteractor {

    public IRequestListener iRequestListener;

    public CommonRequestInteractorImpl(IRequestListener iRequestListener) {
        this.iRequestListener = iRequestListener;
    }

    @Override
    public void request(final int eventTag, final Context context, String url, RequestParams params) {

        System.out.println(url);
        System.out.println(params.toString());

        if (!params.has("accountName")){
            String data = PreferenceUtils.getPrefString(context, Contants.Preference.user_info, "");
            if (!data.equals("")) {
                JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
                UserInfo userInfo = jsonHelper.getData(data, "result");

                if(userInfo != null){
                    params.put("accountName", userInfo.getAccountName());
                }
            }
        }



        AsyncHttpHelper.post(context, url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                System.out.println(new String(responseBody));

                if(!isActivityEnd(context)) {
                    iRequestListener.onSuccess(eventTag, new String(responseBody));
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                Log.e("error",new String(responseBody));
                if(!isActivityEnd(context)) {
                    iRequestListener.onError(eventTag, context.getResources().getString(R.string.netdisable_or_networkdisable));
                }
            }

            @Override
            public void onStart() {
                super.onStart();
                if(!isActivityEnd(context)) {
                    iRequestListener.isRequesting(eventTag, true);
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if(!isActivityEnd(context)) {
                    iRequestListener.isRequesting(eventTag, false);
                }
            }
        });
    }

    @Override
    public void upload(final int eventTag, final Context context, String url, String[] fileKeys, File[] files, Map<String, String> params) {

//        HttpHelper.getInstance().upload(context, url, fileKeys, files, params, new OkHttpResponseHandler<String>(context) {
//            @Override
//            public void onBefore() {
//                super.onBefore();
//                if(!isActivityEnd(context)) {
//                    iRequestListener.isRequesting(eventTag, true);
//                }
//            }
//
//            @Override
//            public void onAfter() {
//                super.onAfter();
//                if(!isActivityEnd(context)) {
//                    iRequestListener.isRequesting(eventTag, false);
//                }
//            }
//
//            @Override
//            public void onResponse(Request request, String json) {
//                super.onResponse(request, json);
//
//                System.out.println(json);
//
//                if(!isActivityEnd(context)) {
//                    iRequestListener.onSuccess(eventTag, json);
//                }
//            }
//
//            @Override
//            public void onError(Request request, Exception e) {
//                super.onError(request, e);
//
//                if(!isActivityEnd(context)) {
//                    iRequestListener.onError(eventTag, Contants.NetStatus.NETDISABLEORNETWORKDISABLE);
//                }
//            }
//        });
    }

    /**
     * 根据context判断activity是否已经结束
     * @param context
     * @return
     */
    public boolean isActivityEnd(final Context context){

        if(context != null){

            try {
//                Activity activity = (Activity)context;

                if(context == null){

                    System.out.println("context为null了");

                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return false;
    }
}
